package netty;

public class ChannelHandlerContext {
    private Pipeline pipeline;
    private String name;
    volatile ChannelHandlerContext next;
    volatile ChannelHandlerContext prev;
    private final int executionMask;


    public ChannelHandlerContext(String name, Pipeline pipeline, int executionMask) {
        this.pipeline = pipeline;
        this.name = name;
        this.executionMask = executionMask;
    }

    /**
     * @param next
     * @param msg
     */
    static void invokeChannelRead(final ChannelHandlerContext next, Object msg) {
        next.invokeChannelRead(msg);
    }

    private void invokeChannelRead(Object msg) {
        channelRead(this, msg);
        fireChannelRead(msg);
    }

    private void fireChannelRead(Object msg) {
        ChannelHandlerContext ctx = findContext(32);
        ctx.channelRead(ctx, msg);
    }

    private void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.printf("ctx=%s, msg=%s  \n", ctx, msg);
    }

    private ChannelHandlerContext findContext(int mask) {// 这个是从链表里面获取一个元素 那么是怎么添加进去的呢？
        System.out.println("findContextInbound-_-");
        ChannelHandlerContext ctx = this;
        do {
            ctx = ctx.next;
        } while ((ctx.executionMask & mask) == 0);
        return ctx;
    }
}
